package com.mmc.web.controller.email;


import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.mail.MessagingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmc.domain.email.MailBean;
import com.mmc.service.email.EmailService;
import com.mmc.service.email.EmailServiceAPI;
import com.mmc.web.controller.base.BaseController;
@Controller
@RequestMapping("/email")
public class EmailController extends BaseController{
	@Autowired
	private EmailService emailService;
	@Autowired
	private EmailServiceAPI emailServiceAPI;
	/**
	 * 列表
	 * @param mailBean
	 * @return
	 */
	@RequestMapping("/list.action")
	public String list(){
		//TODO 给mailBean设置 userId
		List<MailBean> mails = emailService.getAllMails("1");
		model.addAttribute("datas", mails);
		return "/email/list.jsp";
	}
	@RequestMapping("/create.action")
	public String create(){
		return "/email/create.jsp";
	}
	
	/**
	 * 暂存
	 * @return
	 */
	@RequestMapping("/save.action")
	public void save(MailBean mail){
		emailService.saveEmail(mail);
	}
	
	/**
	 * 保存并发送
	 * @return
	 */
	@RequestMapping("/save_send.action")
	public void save_send(MailBean mail){
		mail.setStatus(2);
		emailService.saveEmail(mail);
		try {
			emailServiceAPI.sendMail(mail);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
